<h5>画像の登録</h5>
<%= form_for(@image, :url => {:action => :upload}, :html => {:multipart => true}) do |f| %>
  <p>
    <%#= file_column_field "image", "filename" %>
    <input name="attachment" type="file" />
  </p>
  <p>
  	画像タイプ
  	<%= f.select(:image_type, MasterImageType.all.collect { |i| [i.name, i.id] }) %>
  </p>
  <p>
    蔵元
  	<%= f.select(:maker, Maker.order(:name_yomi).all.collect { |i| [i.name, i.id] }, :include_blank => true) %>
  </p>
  <p>
  	銘柄
	<%= f.select(:production, Product.order(:name_yomi).all.collect { |i| [i.name, i.id] }, :include_blank => true) %>
  </p>
  <p>
  	備考<br />
  	<%= f.text_area :remarks, :rows => 10 %>
  </p>
  <p>
    <%= f.submit '登録' %>
  </p>
<% end %>
<br />

<!-- 画像プレビュー -->
<script type="text/javascript">
	$(function(){
	  $("#maker").change(function(){
	    ajax_post();
	  });
	  $("#production").change(function(){
	    ajax_post();
	  });
	  $("#image_type").change(function(){
	    ajax_post();
	  });
	});
	
	function ajax_post(){
	  var maker_val = $("#maker").val();
	  var prod_val = $("#production").val();
	  var type_val = $("#image_type").val();
	  $.ajax({
	    url: "/images/get_images",
	    type: 'GET',
	    dataType: 'text',
	    data: {maker:maker_val, production:prod_val, image_type:type_val},
	    success: function(data){
	      if(data.length>0){
	        $("#images").html(data);
	      } else {
	        $("#images").html("no images");
	      }
	    },
	    error: function(){
	      alert("通信に失敗しました");
	    }
	  })
	}
</script>

<h5>画像の確認</h5>
<%= form_tag do %>
   蔵元
  <%= select_tag(:maker, options_for_select(Maker.order(:name_yomi).all.collect {|i| [i.name, i.id]}.insert(0, ''), params[:image].nil? ? 0 : params[:image][:maker])) %>
  <br />
  銘柄
  <%= select_tag(:production, options_for_select(Product.order(:name_yomi).all.collect {|i| [i.name, i.id]}.insert(0, ''), params[:image].nil? ? 0 : params[:image][:production])) %>
  <br />
  画像タイプ
  <%= select_tag(:image_type, options_for_select(MasterImageType.all.collect {|i| [i.name, i.id]}, params[:image].nil? ? 0 : params[:image][:image_type])) %>
  <br />
<% end %>

<div id='images'>
<% if not @images.nil? %>
  <% @images.each do |i| %>
    <%= link_to(raw('<img src="/images/image/thumb/' + i.id.to_s + '.jpg" alt=' + i.id.to_s+' align="left" />'), {:action => :edit, :id => i.id.to_s}) %>
  <% end %>
<% end %>
</div>
